#ifndef _SPI_H_
#define _SPI_H_
#include "system.h"
#include "systick.h"
typedef void(*pSpiFlashInit)(void);
typedef u8(*pSpiFlashSendByte)(u8);
typedef u8(*pSpiFlashReadByte)(void);
typedef enum
{
    SPI_OK = 0,
    SPI_ERROR = 1,
    SPI_BUSY = 2,
    SPI_TIMEOUT = 3
} SPI_STATUS;
typedef struct{
    pSpiFlashInit     PspiFlashInit;
    pSpiFlashSendByte PspiFlashSendByte;
    pSpiFlashReadByte PspiFlashReadByte;
}spi_t;

#define Dummy_Byte 0xFF
#define SPIT_FLAG_TIMEOUT 1000
#define FLASH_SPIx SPI1
#define FLASH_SPI_APBxClock_FUN RCC_APB2PeriphClockCmd
#define FLASH_SPI_CLK RCC_APB2Periph_SPI1

// CS(NSS)引脚 片选选普通GPIO即可
#define FLASH_SPI_CS_APBxClock_FUN RCC_APB2PeriphClockCmd
#define FLASH_SPI_CS_CLK RCC_APB2Periph_GPIOC
#define FLASH_SPI_CS_PORT GPIOA
#define FLASH_SPI_CS_PIN GPIO_Pin_4

// SCK引脚
#define FLASH_SPI_SCK_APBxClock_FUN RCC_APB2PeriphClockCmd
#define FLASH_SPI_SCK_CLK RCC_APB2Periph_GPIOA
#define FLASH_SPI_SCK_PORT GPIOA
#define FLASH_SPI_SCK_PIN GPIO_Pin_5
// MISO引脚
#define FLASH_SPI_MISO_APBxClock_FUN RCC_APB2PeriphClockCmd
#define FLASH_SPI_MISO_CLK RCC_APB2Periph_GPIOA
#define FLASH_SPI_MISO_PORT GPIOA
#define FLASH_SPI_MISO_PIN GPIO_Pin_6
// MOSI引脚
#define FLASH_SPI_MOSI_APBxClock_FUN RCC_APB2PeriphClockCmd
#define FLASH_SPI_MOSI_CLK RCC_APB2Periph_GPIOA
#define FLASH_SPI_MOSI_PORT GPIOA
#define FLASH_SPI_MOSI_PIN GPIO_Pin_7

#define FLASH_SPI_CS_LOW() GPIO_ResetBits(FLASH_SPI_CS_PORT, FLASH_SPI_CS_PIN)
#define FLASH_SPI_CS_HIGH() GPIO_SetBits(FLASH_SPI_CS_PORT, FLASH_SPI_CS_PIN)
#define SPI_FLASH_PerWritePageSize 256
#define SPI_FLASH_PageSize 256

#ifndef ELECT
void spiFlashInit(void);
u8 spiFlashSendByte(u8 byte);
u8 spiFlashReadByte(void);
#endif 
spi_t * spiCreate(void);
#endif
